<template>
  <div class="mod-config exam-box">
    <div class="exam-left">
      <paper-cate
        ref="paperCate"
        rootName="考试分类管理"
        :setCurrent="setCurrent"
        :current="current"
        :url="{
                                type: 1,
                                cateType: 1,
                                update:'/exam-admin/exam/cate/update',
                                save:'/exam-admin/exam/cate/save',
                                delete:'/exam-admin/exam/cate/delete',
                                tree:'/exam-admin/exam/cate/tree'
                                }"
      />
    </div>
    <div class="exam-right" ref="fixedMain">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList(1)"
        ref="fixedForm"
      >
        <el-form-item>
          <el-input
            v-model="dataForm.name"
            placeholder="请输入考试名称"
            clearable
            @clear="getDataList(1)"
          />
        </el-form-item>
                <el-form-item>
                  <el-radio-group v-model="dataForm.type" @change="getDataList(1)">
                    <el-radio-button label>全部</el-radio-button>
                    <el-radio-button label="1">模拟</el-radio-button>
                    <el-radio-button label="2">正式</el-radio-button>
<!--                    <el-radio-button label="3">三违</el-radio-button>-->
                  </el-radio-group>
                </el-form-item>
        <el-form-item>
          <el-radio-group v-model="dataForm.status" @change="getDataList(1)">
            <el-radio-button label>全部</el-radio-button>
            <el-radio-button label="1">已发布</el-radio-button>
            <el-radio-button label="0">未发布</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(1)">查询</el-button>
          <el-button v-if="isAuth('exam:exam:save')" type="primary" @click="selectOrGroup=true">新增</el-button>
          <el-button @click="isdeleteHandle()" type="primary" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        width="100%"
        style="width: 100%;"
      >
        <el-table-column fixed type="selection" header-align="center" align="center" width="50"/>
        <el-table-column align="center" type="index" header-align="center" width="80px" label="ID"/>
        <el-table-column
          prop="status"
          label="发布状态"
          width="100px"
          header-align="center"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              v-if="parseInt(scope.row.status) === 0"
              @click="evaluateHandle(scope.row.id)"
            >发布
            </el-button>
            <el-tag v-else>已发布</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          min-width="100"
          label="考试名称"
        />
        <!--        <el-table-column prop="name" header-align="center" align="center" width="100" label="考试类型">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag v-if="parseInt(scope.row.type) === 1">正式考试</el-tag>-->
        <!--            <el-tag v-if="parseInt(scope.row.type) === 2">模拟考试</el-tag>-->
        <!--            <el-tag v-if="parseInt(scope.row.type) === 3">三违考试</el-tag>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column
          prop="duration"
          header-align="center"
          align="center"
          width="80"
          label="时长(分钟)"
        />
        <el-table-column
          prop="duration"
          header-align="center"
          align="center"
          width="80"
          label="来源"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.isExternal === 1">外部</span>
            <span v-if="scope.row.isExternal === 0">内部</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="passMark"
          header-align="center"
          align="center"
          width="80"
          label="及格分数"
        />
        <el-table-column
          prop="startTime"
          header-align="center"
          align="center"
          width="90"
          label="开始时间"
        >
          <template slot-scope="scope">
            <!--            <template v-if="parseInt(scope.row.type) !==2">{{scope.row.startTime.split(' ')[0]}}</template>-->
            <template v-if="scope.row.startTime !==''">{{scope.row.startTime}}</template>
            <template v-else>~</template>
          </template>
        </el-table-column>
        <el-table-column
          prop="endTime"
          header-align="center"
          align="center"
          width="90"
          label="结束时间"
        >
          <template slot-scope="scope">
            <!--            <template v-if="parseInt(scope.row.type) !==2">{{scope.row.endTime.split(' ')[0]}}</template>-->
            <template v-if="scope.row.endTime !==''">{{scope.row.endTime}}</template>
            <template v-else>~</template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
          <!--  0: 未发布； 1：已发布； -->
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <el-button type="text" v-if="scope.row.isExternal !==1" @click="getExamInfo(scope.row.id)" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="积分设置" placement="top">
              <el-button type="text" v-if="parseInt(scope.row.status) !== 0" @click="getpoints(scope.row.id, 500000)" icon="el-icon-setting"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="考试成绩" placement="top">
              <el-button type="text" v-if="parseInt(scope.row.status) !== 0" @click="getExamGrade(scope.row.id)" icon="el-icon-s-platform"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="人工阅卷" placement="top">
              <el-button type="text" @click="peopleReview(scope.row.id)" icon="el-icon-s-custom"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button type="text" @click="deleteHandle(scope.row.id)" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="fixedPag"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>

    <el-dialog
      title="请选择新增类型"
      :visible="selectOrGroup"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      width="40%"
      :before-close="_=>{this.selectOrGroup=false}"
    >
      <el-container>
        <el-main>
          <div
            style="display: flex;align-items: center;justify-content: center;margin-bottom: 50px"
          >
            <!--            <el-radio-group v-model="testType">-->
            <!--              <el-radio-button :label="1">选择</el-radio-button>-->
            <!--              <el-radio-button :label="1">正式考试</el-radio-button>-->
            <!--              <el-radio-button :label="2">模拟考试</el-radio-button>-->
            <!--              <el-radio-button :label="3">三违考试</el-radio-button>-->
            <!--            </el-radio-group>-->
            <div>请进行选择</div>
          </div>
          <ul class="select-or-group">
            <li>
              <el-button type="info" plain @click="selectExamFn()">选择已有试卷</el-button>
            </li>
            <li>
              <el-button type="info" plain @click="groupExamFn()">选择临时组卷</el-button>
            </li>
          </ul>
        </el-main>
      </el-container>
    </el-dialog>

    <el-drawer
      title="选择已有试卷"
      :visible="select"
      direction="btt"
      :before-close="_=>{this.select=false}"
      size="100%"
      custom-class="drawer-box"
    >
      <el-card class="kxy-card">
        <paper
          type="list"
          :setExamRow="setExamRow"
          :handleClose="_=>{this.select=false}"
          :setExam="setExam"
        />
      </el-card>
    </el-drawer>

    <add-exam
      ref="groupExam"
      v-show="groupExam"
      :setExamRow="setExamRow"
      :setExam="setExam"
      :testType="testType"
    />
    <review ref="review"></review>
    <Setpoints ref="setpoints"></Setpoints>
    <el-drawer
      title="配置考试信息"
      custom-class="drawer-box"
      :append-to-body="true"
      :visible="examInfoDrawer"
      size="80%"
      :close-on-press-escape="false"
      :show-close="false"
      :wrapperClosable="false"
    >
      <exam-info-form
        ref="examInfoForm"
        :examInfoFormSubmit="examInfoFormSubmit"
        :setTestType="setTestType"
        :selectExamFn="selectExamFn"
        :testType="testType"
        :handleClose="_=>{this.examInfoDrawer = false}"
        :setExamRow="setExamRow"
        :examRow="examRow"
      />
    </el-drawer>

    <el-drawer
      title="获取考试成绩"
      custom-class="drawer-box"
      :append-to-body="true"
      :visible="examGradeDrawer"
      size="80%"
      :close-on-press-escape="false"
      :show-close="false"
      :wrapperClosable="false"
      direction="ttb"
      v-loading="examGradeLoading"
    >
      <get-exam-grade
        ref="examGrade"
        :handleClose="_=>{this.examGradeDrawer = false}"
        :setLoading="setLoading"
      />
    </el-drawer>
  </div>
</template>

<script>
  import AddOrUpdate from './exam-add-or-update'
  import SchoolReport from './exam-schoolreport'
  import paperCate from './../paper/papercate'
  import paper from './../paper/paper'
  import ExamInfoForm from './exam-info-form'
  import AddExam from '../../../components/check/addExam'
  import GetExamGrade from './get-exam-grade'
  import Review from './review'
  import Setpoints from './setpoints'

  export default {
    data() {
      return {
        testType: 1,
        groupExam: false,
        examRow: {
          paperName: '',
          id: null
        },
        select: false,
        group: false,
        examInfoDrawer: false,
        examGradeDrawer: false,
        selectOrGroup: false,
        dialogFormVisible: false,
        current: {},
        paperCate: false,
        dataForm: {
          name: '',
          type: '',
          cateId: '',
          status: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        // addOrUpdateVisible: false,
        schoolReportVisible: false,
        examGradeLoading: false
      }
    },
    components: {
      Setpoints,
      GetExamGrade,
      AddExam,
      ExamInfoForm,
      paper,
      paperCate,
      AddOrUpdate,
      Review,
      SchoolReport
    },
    mounted() {
      this.$refs.paperCate.init()
      this.getDataList()
    },
    methods: {
      setTestType(val) {
        this.testType = val
      },
      setLoading(loading) {
        this.examGradeLoading = loading
      },
      groupExamFn(data) {
        this.group = true
        this.groupExam = true
        this.$nextTick(_ => this.$refs.groupExam.init(data))
      },
      peopleReview(id) {
        this.$nextTick(_ => this.$refs.review.init(id))
      },
      // 积分设置
      getpoints(id, pCode) {
        this.$nextTick(_ => this.$refs.setpoints.init(id, pCode))
      },
      selectExamFn() {
        this.select = true
      },
      setExam() {
        if (this.examRow && this.examRow.paperName) {
          this.getExamInfo()
        } else {
          this.$notify.error({
            title: '错误',
            message: '请先选择试卷'
          })
        }
      },
      examInfoFormSubmit() {
        this.examInfoDrawer = false
        this.$refs.groupExam.examDialogVisible = false
        this.select = false
        this.selectOrGroup = false
        this.getDataList(1)
      },
      //  单选
      setExamRow(val) {
        this.examRow = val
      },
      delectCate() {
        this.current = {name: '', id: ''}
        this.dataForm.cateId = ''
        this.getDataList()
      },
      setCurrent(data, bool) {
        if (bool && data.id) {
          this.current = data
          this.dataForm.cateId = data.id
          this.getDataList(1)
        }
      },
      // 获取数据列表
      getDataList(page) {
        if (page) this.pageIndex = page
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/exam-admin/exam/list'),
          method: 'post',
          data: {
            page: this.pageIndex,
            limit: this.pageSize,
            name: this.dataForm.name,
            type: this.dataForm.type,
            status: this.dataForm.status,
            cateId: this.dataForm.cateId
          }
        }).then(res => {
          if (res && res.code === 200) {
            this.dataList = res.data.list
            this.totalPage = res.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 修改
      getExamInfo(id) {
        if (this.examInfoDrawer) {
          this.select = false
        } else {
          this.examInfoDrawer = true
          this.$nextTick(_ => {
            this.$refs.examInfoForm.getExamInfo(id, 1)
          })
        }
      },

      // 查看考试成绩
      getExamGrade(id) {
        this.examGradeDrawer = true
        this.$nextTick(() => {
          this.$refs.examGrade.getExamId(id)
        })
      },
      // 发布培训
      evaluateHandle(id) {
        var that = this
        that
          .$alert('确定要发布考试吗？', '提示', {
            confirmButtonText: '发布',
            type: 'warning'
          })
          .then(() => {
            that
              .$http({
                url: that.$http.adornUrl(
                  // `/exam-admin/exam/${'publishExam'}`
                  '/exam-admin/exam/editexamstatus'
                ),
                method: 'post',
                data: {
                  id: id,
                  status: 1
                }
              })
              .then(data => {
                if (data && data.code === 200) {
                  that.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  that.getDataList()
                } else {
                  that.$message.error(data.msg)
                }
              })
          })
          .catch(() => {
            that.$message({
              type: 'info',
              message: '已取消发布'
            })
          })
      },
      isdeleteHandle() {
        let ids = this.dataListSelections.map(x => x.id).join(',')
        this.$prompt('请输入DEL以确认进行删除操作', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValidator(val) {
            return val.toUpperCase() === 'DEL'
          },
          inputErrorMessage: '输入错误'
        })
          .then(({value}) => {
            // let formData = new FormData()
            // formData.append('ids', ids)
            this.$http({
              url: this.$http.adornUrl('/exam-admin/exam/batchDel'),
              method: 'post',
              data: {
                id: ids,
                confirm: value
              }
            }).then(data => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.getDataList()
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '取消输入'
            })
          })
      },
      // 删除
      deleteHandle(id) {
        this.$prompt('请输入DEL以确认进行删除操作', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValidator(val) {
            return val.toUpperCase() === 'DEL'
          },
          inputErrorMessage: '输入错误'
        })
          .then(({value}) => {
            // let formData = new FormData()
            // formData.append('ids', ids)
            this.$http({
              url: this.$http.adornUrl('/exam-admin/exam/delete'),
              method: 'post',
              data: {
                id: id,
                confirm: value
              }
            }).then(data => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex--
                this.getDataList()
              }
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '取消输入'
            })
          })
      }
    }
  }
</script>
<style lang="scss">
  .select-or-group {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    justify-content: space-around;
    align-items: center;

    li {
      flex: 1;
      font-size: 14px;
      text-align: center;
    }
  }
 .item {
   font-size: 20px;
 }
  .exam-box {
    height: 100%;
    display: flex;
    overflow: auto;
    border: 1px solid #eee;

    .exam-left {
      padding-right: 10px;
    }

    .exam-right {
      padding: 10px;
      overflow: auto;
      flex: 1;
    }
  }
</style>
